package com.van.service.sys.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.van.entity.account.RolePermission;
import com.van.entity.sys.Menu;
import com.van.mapper.sys.MenuMapper;
import com.van.service.sys.MenuService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @author VanKa qq:2395073039
 * @date 2021/6/28 10:11
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    @Override
    public List<Menu> show() {
        List<Menu> list = list();
        if (list == null){
            return null;
        }
        List<Menu> headline = new ArrayList<>(); // 标题列表
        for (Menu menu : list){
            if (menu.getMId() == 0){
                headline.add(menu);
            }
        }

        if (headline == null){
            return null;
        }

        List<Menu> result = new ArrayList<>(); // 返回标题列表
        for (Menu menu : headline){
            List<Menu> subclass = new ArrayList<>(); // 子标题列表
            for (Menu m : list){
                if (m.getMId() == menu.getId()){
                    subclass.add(m);
                }
            }
            //排序
            Collections.sort(subclass, new Comparator<Menu>() {
                @Override
                public int compare(Menu o1, Menu o2) {
                    return o1.getSort() - o2.getSort();
                }
            });
            menu.setData(subclass);
            result.add(menu);
        }
        //排序
        Collections.sort(result, new Comparator<Menu>() {
            @Override
            public int compare(Menu o1, Menu o2) {
                return o1.getSort() - o2.getSort();
            }
        });

        return result;
    }

    @Override
    public List<Menu> getShow(List<Menu> show, List<RolePermission> list) {
        if (list.size() == 0){
            return show;
        }
        List<Menu> data = new ArrayList<>();
        for (Menu menu : show){
            List<Menu> d = new ArrayList<>();
            for (Menu m : menu.getData()){
                for (RolePermission r : list){
                    if (r.getPermissionId().equals(m.getId())){
                        m.setOwn(true);
                    }
                }
                d.add(m);
            }
            menu.setData(d);
            data.add(menu);
        }
        return data;
    }

    @Override
    public List<Menu> getMenu(List<Menu> show, List<RolePermission> list) {
        if (list.size() == 0){
            return show;
        }
        List<Menu> data = new ArrayList<>();
        for (Menu menu : show){
            List<Menu> d = new ArrayList<>();
            boolean on = false;
            for (Menu m : menu.getData()){
                for (RolePermission r : list){
                    if (r.getPermissionId().equals(m.getId())){
                        d.add(m);
                        on = true;
                    }
                }
            }
            if (on){
                menu.setData(d);
                data.add(menu);
            }
        }
        return data;
    }
}
